﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.ApplicationBlocks.Data;
using System.Configuration;
namespace ModuleDuoc.DM
{
    public partial class frmDM_TheKho : WeifenLuo.WinFormsUI.Docking.DockContent 
    {
        public frmDM_TheKho()
        {
            InitializeComponent();
        }

        private void frmDM_TheKho_Load(object sender, EventArgs e)
        {
            this.loadLoaiThuoc();
            this.dgvTheKho.AutoGenerateColumns = false;
            this.dgvData.AutoGenerateColumns = false;
        }
        private void loadLoaiThuoc()
        {
            string sql = "select MANHOM,TENNHOM from zD_DM_VATTU_NHOMVATTU union select '',N'--Tất cả--'";
            DataTable dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                this.cboNhomThuoc.DataSource = dt;
                this.cboNhomThuoc.ValueMember = "MANHOM";
                this.cboNhomThuoc.DisplayMember = "TENNHOM";
                this.cboNhomThuoc.SelectedValue = "";
            }
        }
        private void loadThuoc(string where)
        {
            string sql = "select MAVT,TENVT from zD_DM_THUOC " + where;
            DataTable dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                this.dgvData.DataSource = dt;
            }
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            if(this.txtTenThuoc.Text.Trim().Length>0)
            {
                if (this.cboNhomThuoc.SelectedValue.ToString() == "")
                {
                    this.loadThuoc("where TENVT like '%" + this.txtTenThuoc.Text.Trim() + "%'");
                }
                else
                {
                    this.loadThuoc("where TENVT like '%" + this.txtTenThuoc.Text.Trim() + "%' and MANHOMVATTU='" + this.cboNhomThuoc.SelectedValue.ToString() + "'");
                }
            }
        }

        private void cboNhomThuoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.loadThuoc("");
        }

        private void dgvData_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            getTheKHo();
        }

        private void getTheKHo()
        {
            try
            {
                string sql = "select * from view_TheKho where MAVT='" + dgvData["MAVT", dgvData.CurrentRow.Index].Value.ToString() + " Group by mavach'";
                DataTable dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
                dgvTheKho.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông báo");
            }
        }

        private void dgvData_Leave(object sender, EventArgs e)
        {
            getTheKHo();
        }

        private void dgvData_KeyDown(object sender, KeyEventArgs e)
        {
            getTheKHo();
        }

        private void txtTenThuoc_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                this.getTheKHo();
        }
    }
}
